package cn.com.ylpw.web.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author ZJJ
 * @Date 2010-2-1
 * @Time 下午02:41:26 类说明
 */
public class PropertiesUtils {
	
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

	private String propertiesName;

	public PropertiesUtils(String properties) {
		this.propertiesName = properties;
	}

	/* 根据Key 读取Value */
	public String getValue(String key) {
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(this.propertiesName);
		Properties p = new Properties();
		try {
			p.load(inputStream);
			return p.getProperty(key);
		} catch (IOException e) {
			logger.error("根据Key 读取Value异常", e);
			return null;
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
	}

	public String jsp_getValue(String key) {
		FileInputStream in = null;
		try {
			// TODO 这里可能读取不到配置文件
			in = new FileInputStream(this.propertiesName);
			Properties p = new Properties();
			p.load(in);
			return p.getProperty(key);
		} catch (Exception e) {
			logger.error("根据Key 读取Value异常", e);
			return null;
		} finally {
			IOUtils.closeQuietly(in);
		}
	}

	/* 修改或添加键值对 如果key存在，修改 反之，添加。 */
	public void jsp_writeData(String key, String value) {
		Properties p = new Properties();
		//String path = this.propertiesName;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			File file = new File(this.propertiesName);
			if (!file.exists())
				file.createNewFile();
			inputStream = new FileInputStream(this.propertiesName);
			p.load(inputStream);
			outputStream = new FileOutputStream(this.propertiesName);
			p.setProperty(key, value);
			p.store(outputStream, "Item");
		} catch (IOException e) {
			logger.error("Visit{} for updating {} value error{} ",this.propertiesName,value, e);
		} finally {
			IOUtils.closeQuietly(outputStream);
			IOUtils.closeQuietly(inputStream);
		}
	}

	/* 修改或添加键值对 如果key存在，修改 反之，添加。 */
	public void writeData(String key, String value) {
		Properties p = new Properties();
		String path = p.getClass().getResource("/").getPath().substring(1, p.getClass().getResource("/").getPath().length()) + this.propertiesName;
		//String path = this.propertiesName;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			File file = new File(path);
			if (!file.exists())
				file.createNewFile();
			inputStream = this.getClass().getClassLoader().getResourceAsStream(this.propertiesName);
			p.load(inputStream);
			outputStream = new FileOutputStream(path);
			p.setProperty(key, value);
			p.store(outputStream, "Item");
		} catch (IOException e) {
			logger.error("Visit{} for updating {} value error{} ",this.propertiesName,value, e);
		} finally {
			IOUtils.closeQuietly(outputStream);
			IOUtils.closeQuietly(inputStream);
		}
	}

	public void setPropertiesName(String propertiesName) {
		this.propertiesName = propertiesName;
	}
	
	/**
	 * @description 
	 * 功能描述: Properties 一级目录 [Map] aaa=1,bbb=2 
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 
	 * @return       返回类型: 
	 * @createdate   建立日期：Jul 24, 20138:28:07 PM
	 */
	public Map<String,String> toMap(){
		return get(this.propertiesName);
	}
	
	/**
	 * @description 
	 * 功能描述: Properties 二级目录  [Map包含Map] aaa.a1,aaa.a2,aaa.a3
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 
	 * @return       返回类型: 
	 * @createdate   建立日期：Jul 24, 20138:28:14 PM
	 */
	public Map<String,Map<String,String>> toMapContainMap(){
		return getByType(this.propertiesName);
	}
	
	
	/**
	 * 获取以.为分割的区域类别划分信息，如db1.uname,db1.url;db2.uname,db2.url;
	 * @param propertiesName 资源文件名称
	 * @return 返回  Map 类型的数据结果
	 */
	public static Map<String,Map<String,String>> getByType(String propertiesName){
		Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();
		Properties properties = new Properties();
		InputStream inputStream = null;
		try {
			inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName);
			properties.load(inputStream);
		} catch (IOException e) {
			logger.error("获取以.为分割的区域类别划分信息",e);
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
		Map<String,String> values = null;
		for(Object key : properties.keySet()){
			String property = (String)key;
			int dotIndex = property.indexOf('.');
			String newType = property.substring(0,dotIndex);//类型名
			String value = property.substring(dotIndex+1,property.length());//同类型下的属性名
			String proValue = properties.getProperty(property);//属性值
			values = map.get(newType);
			if(values==null){
				values =new HashMap<String,String>();
			}
			values.put(value, proValue);
			map.put(newType, values);
		}
		return map;
	}
	
	/**
	 * 获取properties文件属性
	 * @param propertiesName 资源文件名称
	 * @return 返回  Map 类型的数据结果
	 */
	public static Map<String,String> get(String propertiesName){
		Map<String,String> map = new HashMap<String, String>();
		Properties properties = new Properties();
		InputStream inputStream = null;
		try {
			inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName);
			properties.load(inputStream);
		} catch (IOException e) {
			logger.error("获取properties文件属性{}",propertiesName,e);
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
		for(Object key : properties.keySet()){
			String property = (String)key;
			String proValue = properties.getProperty(property);
			map.put(property, proValue);
		}
		return map;
	}

}
